package com.app.business.service;

import com.app.business.model.AzProject;
import com.app.business.model.UserInfoForProperty;
import com.app.system.jpa.JpaBaseDao;
import com.app.system.permission.model.SysRole;
import com.app.system.permission.model.UserInfo;
import com.app.system.permission.service.UserInfoService;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.dataType.StringUtils;
import com.app.system.utils.exception.Precondition;
import com.app.system.utils.exception.Response;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 安置房源分配
 * Created by yby on 2019/2/14 17:56.
 */
@Service
@Transactional
public class UserInfoForPropertyService extends JpaBaseDao<UserInfoForProperty, Integer> {

    @Autowired
    private HouseService houseService;
    @Autowired
    private AzProjectService azProjectService;
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 根据Id得到
     *
     * @param
     * @return
     */
    public List<SysRole> getSysRoleListForProperty() {
        //查询语句
        StringBuffer hql = new StringBuffer(" from SysRole ss  where ss.role like '物业%'");

        List<SysRole> sysRoleList = entityManager.createQuery(hql.toString()).getResultList();
        return sysRoleList;


    }

    public List<Map> getPropertyForHouseNum(String town) {
        List<Map> result = Lists.newArrayList();
        List<AzProject> azProjectList = this.azProjectService.azProjectByTownList(town);
        Map map = null;
        for (AzProject azProject : azProjectList) {
            map = null;
            map = Maps.newHashMap();

            map.put("id", azProject.getId());
            map.put("pid", 0);
            map.put("name", azProject.getProjectName());
            result.add(map);
            List<String> buildNumList = this.houseService.getBuildNumListByAzProjectId(azProject.getId());
            for (String buildNum : buildNumList) {
                map = null;
                map = Maps.newHashMap();

                map.put("id", azProject.getId() + "_" + buildNum);
                map.put("pid", azProject.getId());
                map.put("name", buildNum);
                result.add(map);

            }
        }
        return result;
    }

    public List<String> findByUserIdAndTowm(int userInfoId) {
        StringBuffer sql = new StringBuffer("select CONCAT(az_project_id,'_',build_num) from user_Info_For_Property u where u.user_info_id = :userInfoId order by u.id");
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("userInfoId", userInfoId);
        return this.findSqlObject(sql.toString(), paramMap);
    }

    public List<String> findAzProjectIdByUserId(int userInfoId) {
        StringBuffer sql = new StringBuffer("select az_project_id from user_Info_For_Property u where u.user_info_id = :userInfoId order by u.id");
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("userInfoId", userInfoId);
        return this.findSqlObject(sql.toString(), paramMap);
    }


    public List<String> findByUserIdAndAzProjectId(int userInfoId,int azProjectId) {
        StringBuffer sql = new StringBuffer("select u.build_num from user_Info_For_Property u where u.user_info_id = :userInfoId and u.az_project_id = :azProjectId order by u.id");
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("userInfoId", userInfoId);
        paramMap.put("azProjectId", azProjectId);
        return this.findSqlObject(sql.toString(), paramMap);
    }

    public List<Integer> findAzProjectIdByUserIdAndTowm(int userInfoId) {
        StringBuffer sql = new StringBuffer("select az_project_id from user_Info_For_Property u where u.user_info_id = :userInfoId group by az_project_id");
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("userInfoId", userInfoId);
        return this.findSqlObject(sql.toString(), paramMap);
    }

    /**
     * 保存菜单权限分配
     */
    @Transactional
    public synchronized Response saveAzProjectNamePermission(HttpServletRequest request) {
        Integer userInfoId = ParamUtils.getInt(request, "userInfoId", 0);
        String town = ParamUtils.getString(request, "townName", "");
        String authids = ParamUtils.getString(request, "authids", "");
        Precondition.checkAjaxArguement(userInfoId != 0 && town != "" && town != null, "1111", "数据错误，请重新操作！");
        UserInfo userInfo=this.userInfoService.findOneById(userInfoId);
        List<Integer> anProjectIdList = this.azProjectService.getAzProjectIdList(town);
        this.deleteByUserInfoId(userInfoId, anProjectIdList);
        if (StringUtils.isNotEmpty(authids)) {
            String[] authArray = authids.split(",");
            for (String s : authArray) {
                if (s.contains("_")) {
                    UserInfoForProperty userInfoForProperty = new UserInfoForProperty();
                    String ss[] = s.split("_");
                    userInfoForProperty.setAzProjectId(Integer.valueOf(ss[0]));
                    userInfoForProperty.setBuildNum(ss[1]);
                    userInfoForProperty.setUserInfoId(userInfo.getId());
                    this.save(userInfoForProperty);
                }
            }
        }
        return new Response("0001", "设置成功！");
    }

    /**
     * 通过用户id删除UserInfoForProperty信息
     *
     * @param userInfoId
     */
    @Transactional
    public synchronized void deleteByUserInfoId(Integer userInfoId, List<Integer> anProjectIdList) {
        String sql = "delete from User_Info_For_Property where user_info_id =:userInfoId and az_project_id in (:anProjectIdList) ";
        Map<String, Object> param = Maps.newHashMap();
        param.put("userInfoId", userInfoId);
        param.put("anProjectIdList", anProjectIdList);
        this.executeSql(sql, param);
    }

}
